package com.cyn.controller.rabbitmq.config;
 
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate.ConfirmCallback;
import org.springframework.amqp.rabbit.core.RabbitTemplate.ReturnCallback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 

@Component
public class Send2 implements ConfirmCallback,ReturnCallback {
 
	private RabbitTemplate rabbitTemplate;
	
	@Autowired
	public Send2(RabbitTemplate rabbitTemplate) {
		this.rabbitTemplate = rabbitTemplate;
		rabbitTemplate.setConfirmCallback(this); 
		rabbitTemplate.setReturnCallback(this);
	}
	
	@Override
		public void confirm(CorrelationData correlationData, boolean ack, String cause) {
		   System.err.println("correlationData：" + correlationData);
		   String massageID = correlationData.getId();
			System.err.println("===massageID：" + massageID);
			if (!ack) {
				System.err.println("ACK：" + ack+ "===cause：" + cause);
			} else {
				System.err.println("ACK：" + ack );
			}
		}
 
 
	// 交换机到队列报错执行回调
	
		@Override
		public void returnedMessage(Message message, int replyCode, String replyText, String exchange,
				String routingKey) {
			System.err.println("==========消息主体=========" + message);
			System.err.println("==========消息主体=========" + replyCode);
			System.err.println("==========描述=========" + replyText);
			System.err.println("==========交换机=========" + exchange);
			System.err.println("==========路由键=========" + routingKey);
 
		}
 
	
	public void SendOrder(Order order) throws Exception {
		CorrelationData correlationData = new CorrelationData();
		correlationData.setId(order.getMessageId());
		rabbitTemplate.convertAndSend("order-exchange", "order.add", order, correlationData);
		
 
	}
 
	
 
}